11th June 2020
Magento 2 Direct SQL Queries in Zend Format


Sometimes due to some project requirements, we need to directly communicate with Magento2 Database and we have to write SQL queries for that. This post will help you to write direct SQL Queries in Zend Format.
public function __construct( \Magento\Framework\App\ResourceConnection $resourceConnection ) { $this->resourceConnection = $resourceConnection; }
$connection = $this->resourceConnection->getConnection(); $tableName = $this->resourceConnection->getTableName('customer_history'); $tableName2 = $this->resourceConnection->getTableName('order_history');
OR
Using Object Manager Directly:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName =$resource->getTableName('customer_history');
$tableName2 =$resource->getTableName('order_history');
Select Queries:
Syntax:
$select = $connection->select() ->from( ['p' => $tableName]) ->where('p.column_name=?', $value) ->where('p.column_name2 >=?', $value) ->order('p.column_name3 DESC') ->limit($pagesize, $offset); $data = $connection->fetchAll($select);
Example:
$select = $connection->select() ->from( ['p' => $tableName]) ->where('p.customer_id=?', 5) ->where('p.status =?', 1) ->order('p.createdat DESC') ->limit(10, 0); To get only selected columns from Table: $select = $connection->select() ->from( ['p' => $tableName],['p.order_id', 'p.status']) ->where('p.customer_id=?', 5) ->where('p.status =?', 1) ->order('p.createdat DESC') ->limit(10, 0); $data = $connection->fetchAll($select);
Perform Select Query with JOIN
$select = $connection->select() ->from( ['p' => $tableName], ['p.customer_id', 'p.status', 'p.order_id','o.ordertotal'] ) ->join( ['o' => $tableName2], 'main_table.order_id = order.id', [''] ) ->where('p.order_id = (?)', '455'); $data = $connection->fetchAll($query);
Insert Query:
Syntax:
$data = ["column_name"=>$value,"column_name2"=>$value2,'column_name3'=>$value3]; $connection->insert($tableName, $data);
Example :
$data = ["customer_id"=>'22',"status"=>1,'order_id'=>'455']; $lastInsertedID= $connection->insert($tableName, $data);
Update Query:
Syntax:
$data = ["column_name3"=>$value3]; $where = ['column_name = ?' => $value, 'column_name2 = ?' => $value2]; $updatedRows=$connection->update($tableName, $data, $where);
Example:
$data = ["status"=>1]; $where = ['customer_id = ?' => '22', 'order_id = ?' => '455']; $updatedRows=$connection->update($tableName, $data, $where);
Delete Queries
Syntax:
$connection->delete( $tableName, ['column_name = ?' => $value, 'column_name2 = ?' => '0'] );
Example :
$connection->delete( $tableName, ['customer_id = ?' => '22', 'status = ?' => '0'] );

Acknowledges for paper such a beneficial composition, I stumbled beside your blog besides decipher a limited announce. I want your technique of inscription... بنشر متنقل

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. Alexistogel

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. timur188

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. 마닐라 여행

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. bandar slot

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. cheapest Atlas mountain hike activity

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. Nusantara4d

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. manaplay

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. ดูบอลสด

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading.광주 출장마사지

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. ข่าวกีฬาลิเวอร์พูล

Wow, cool post. I'd like to write like this too - taking time and real hard work to make a great article... but I put things off too much and never seem to get started. Thanks though. toto macau

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Bursa Boşanma Avukatı

Great survey, I'm sure you're getting a great response. marketing recruiting firms Great survey, I'm sure you're getting a great response. situs slot Great survey, I'm sure you're getting a great response. situs gacor Great survey, I'm sure you're getting a great response. bandar togel Great survey, I'm sure you're getting a great response. situs slot kantorbola login

Great survey, I'm sure you're getting a great response. marketing recruiting firms Great survey, I'm sure you're getting a great response. situs slot Great survey, I'm sure you're getting a great response. situs gacor Great survey, I'm sure you're getting a great response. bandar togel Great survey, I'm sure you're getting a great response. situs slot kantorbola login
Please rotate your device
We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site